package gui;

import gui.events.TablePopupEvent;
import gui.interfaces.IPopupMenuTableEventListener;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JSeparator;

public class PopupMenuTable extends JPopupMenu {
	private List<IPopupMenuTableEventListener> _listener = new ArrayList<IPopupMenuTableEventListener>();

	/**
	 * Constructor
	 */
	public PopupMenuTable() {
		JMenuItem select200Rows = new JMenuItem("Select row - Litmit 200");
		select200Rows.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				fireSetlect200Rows();
			}
		});

		JMenuItem edit200Rows = new JMenuItem("Edit table - Litmit 200");
		edit200Rows.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				fireEdit200Rows();
			}
		});

		JMenuItem createTable = new JMenuItem("Create table");
		createTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				fireCreateTable();
			}
		});
		JMenuItem alterTable = new JMenuItem("Alter table");
		alterTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				fireAlterTable();
			}
		});

		JMenuItem dropTable = new JMenuItem("Drop table");
		dropTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				fireDropTable();
			}
		});
		JMenuItem refreshAll = new JMenuItem("Refresh all");
		refreshAll.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				fireRefreshAll();
			}
		});

		add(select200Rows);
		add(edit200Rows);
		add(new JSeparator());
		add(createTable);
		add(alterTable);
		add(dropTable);
		add(new JSeparator());
		add(refreshAll);
	}

	/**
	 * fire edit 200 first rows event.
	 */
	protected void fireEdit200Rows() {
		TablePopupEvent event = new TablePopupEvent(this);
		Iterator<IPopupMenuTableEventListener> i = _listener.iterator();
		while (i.hasNext()) {
			((IPopupMenuTableEventListener) i.next()).edit200Rows(event);
		}
	}

	/**
	 * fire drop table event
	 */
	protected void fireDropTable() {
		TablePopupEvent event = new TablePopupEvent(this);
		Iterator<IPopupMenuTableEventListener> i = _listener.iterator();
		while (i.hasNext()) {
			((IPopupMenuTableEventListener) i.next()).dropTable(event);
		}
	}

	/**
	 * fire alter table event
	 */
	protected void fireAlterTable() {
		TablePopupEvent event = new TablePopupEvent(this);
		Iterator<IPopupMenuTableEventListener> i = _listener.iterator();
		while (i.hasNext()) {
			((IPopupMenuTableEventListener) i.next()).alterTable(event);
		}
	}

	/**
	 * fire create table event
	 */
	protected void fireCreateTable() {
		TablePopupEvent event = new TablePopupEvent(this);
		Iterator<IPopupMenuTableEventListener> i = _listener.iterator();
		while (i.hasNext()) {
			((IPopupMenuTableEventListener) i.next()).createTable(event);
		}
	}

	/**
	 * fire select 1000 rows event
	 */
	protected void fireSetlect200Rows() {
		TablePopupEvent event = new TablePopupEvent(this);
		Iterator<IPopupMenuTableEventListener> i = _listener.iterator();
		while (i.hasNext()) {
			((IPopupMenuTableEventListener) i.next()).select200Rows(event);
		}
	}

	/**
	 * add listener
	 * 
	 * @param listener
	 */
	public synchronized void addPopupTableEventListener(
			IPopupMenuTableEventListener listener) {
		_listener.add(listener);
	}

	/**
	 * remove listener
	 * 
	 * @param listener
	 */
	public synchronized void removePopupTableEventListener(
			IPopupMenuTableEventListener listener) {
		_listener.remove(listener);
	}

	/**
	 * fire refresh all event
	 */
	public synchronized void fireRefreshAll() {
		TablePopupEvent event = new TablePopupEvent(this);
		Iterator<IPopupMenuTableEventListener> i = _listener.iterator();
		while (i.hasNext()) {
			((IPopupMenuTableEventListener) i.next()).refreshAll(event);
		}
	}
}
